﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using DAO;

namespace QuanLyKhachSan
{
    public partial class FmPhong_DanhSach : Form
    {
        public FmPhong_DanhSach()
        {
            InitializeComponent();
        }

        private void FmPhong_DanhSach_Load(object sender, EventArgs e)
        {
            LoadDanhSachPhong();
        }
        public void LoadDanhSachPhong()
        {
            PhongBUS bus = new PhongBUS();
            
            DataTable dt = new DataTable();
            dt = bus.LoadDanhSachPhongBUS();
            foreach (DataRow row in dt.Rows)
            {
                PhongDTO Phong = new PhongDTO();
                Phong.Maphong = Int32.Parse(row["MaPhong"].ToString());
                Phong.Tenphong = row["TenPhong"].ToString();
                Phong.Tinhtrang = Int32.Parse(row["MaTinhTrang"].ToString());
                ListViewItem item = new ListViewItem(Phong.Tenphong.ToString());
                item.Name = Phong.Maphong.ToString();
                item.Tag=Phong.Tinhtrang.ToString() ;
                if (Phong.Tinhtrang == 1)
                {
                    item.ImageIndex = 1;
                }
                else
                    if (Phong.Tinhtrang == 2)
                    {
                        item.ImageIndex = 2;
                    }
                    else
                    {
                        item.ImageIndex = 3;
                    }
                listPhong.Items.Add(item);
                
            }
        }
        public PhongDTO LayThongTinPhong(int id)
        {
            PhongBUS bus = new PhongBUS();
            DataRow rows = bus.LayThongTinPhongBUS(id);
           return new PhongDTO()
           {
               Dmphong = Int32.Parse(rows["MaDMP"].ToString()),
               Tinhtrang = Int32.Parse(rows["MaTinhTrang"].ToString()),
               Tenphong = rows["TenPhong"].ToString(),
                Maphong = Int32.Parse(rows["MaPhong"].ToString())
            };
        }
        private void listPhong_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                PhongDTO Phong = LayThongTinPhong(int.Parse(listPhong.SelectedItems[0].Name));
                if (Phong.Tinhtrang == 1)// Phong trong
                {
                    StripDatPhong.Visible = true; // cho dat phong
                    StripThuePhong.Text = "Thuê Phòng";
                    StripThuePhong.Visible = true;/// cho thue
                    StripTraPhong.Visible = false;
                    StripChuyenPhong.Visible = false;
                    StripThemDichVu.Visible = false;
                }
                else
                    if (Phong.Tinhtrang == 2)// Phong dang su dung
                    {
                        StripDatPhong.Visible = false;
                        StripThuePhong.Visible = false;
                        StripTraPhong.Visible = true;
                        StripChuyenPhong.Visible = true;
                        StripThemDichVu.Visible = true;
                    }
                    else // phong duoc dat 
                    {
                        StripThuePhong.Text = "Nhận Phòng";
                        StripThuePhong.Visible = true;
                        StripDatPhong.Visible = false;
                        StripTraPhong.Visible = false;
                        StripChuyenPhong.Visible = false;
                        StripThemDichVu.Visible = false;
                    }
                ContextMenuPhong.Show(listPhong,e.X+5,e.Y);
            }
        }

        private void StripThuePhong_Click(object sender, EventArgs e)
        {
            string id = listPhong.SelectedItems[0].Name;
            string tinhtrang = listPhong.SelectedItems[0].Tag.ToString();
            FmPhong_ThuePhong fm = new FmPhong_ThuePhong(id,tinhtrang);
            fm.EventThuesuccess += new FireEventForThuePhongThanhCong(thue_EventThueThanhCong);
            fm.ShowDialog();
        }
        void thue_EventThueThanhCong(object sender, ThueThanhCong e)
        {
            listPhong.BeginUpdate();
            listPhong.SelectedItems[0].ImageIndex = 2;
            listPhong.EndUpdate();
            listPhong.Refresh();
        }
        void thanhtoan_NhanEventThanhToanThanhCong(object sender, ThanhToanThanhCong e)
        {
            listPhong.BeginUpdate();
            listPhong.Clear();
            LoadDanhSachPhong();
            listPhong.EndUpdate();
            listPhong.Refresh();
        }

        private void StripXemSua_Click(object sender, EventArgs e)
        {
            string id = listPhong.SelectedItems[0].Name;
            string tinhtrang = listPhong.SelectedItems[0].Tag.ToString();
            FmPhong_ChiTietPhieuThue fm = new FmPhong_ChiTietPhieuThue(id);
            fm.ShowDialog();
        }

        private void StripThanhToan_Click(object sender, EventArgs e)
        {
            string maphong = listPhong.SelectedItems[0].Name;
            FmHoaDon fm = new FmHoaDon(maphong);
            fm.Show();
            fm.EventThanhToan += new FireEventThanhToan(thanhtoan_NhanEventThanhToanThanhCong);
           
        }

        private void testToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FmHoaDonNhieuPhong fm = new FmHoaDonNhieuPhong();
            fm.Show();
        }
    }
}
